package database

import (
	"fmt"
	"github.com/go-redis/redis"
	"github.com/spf13/viper"
)

var Rdb *redis.Client

func InitRedis() *redis.Client {
	var err error

	host := viper.GetString("redis.host")
	port := viper.GetInt("redis.port")
	database := viper.GetInt("redis.database")
	password := viper.GetString("redis.password")

	Rdb = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", host, port),
		Password: password, // no password set
		DB:       database, // use default DB
	})

	pong, err := Rdb.Ping().Result()
	fmt.Println(pong, err)
	// Output: PONG <nil>

	return Rdb
}
